# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit  eutils fdo-mime autotools multilib cvs flag-o-matic mozilla-launcher mozconfig-2

ECVS_SERVER="cvs-mirror.mozilla.org:/cvsroot"
ECVS_MODULE="mozilla"
#ECVS_BRANCH="MOZILLA_1_8_BRANCH"
#ECVS_TOPDIR="${DISTDIR}/cvs-src/mozilla-cvs"
S=${WORKDIR}/mozilla

DESCRIPTION="Mozilla Thunderbird"
HOMEPAGE="http://www.mozilla.org/projects/firefox/"
#SRC_URI="http://www.ecs.soton.ac.uk/~aa702/${P}.tbz2"

LICENSE="MPL-1.1 NPL-1.1"
SLOT="0"
RESTRICT="nomirror"
KEYWORDS="~amd64 ~x86"

#RDEPEND="!>mail-client/mozilla-thunderbird-1.5"

src_unpack(){
	cvs_src_unpack
	cd ${S}
	epatch "${FILESDIR}/gentoo-cairo.patch"
}

src_compile() {
	declare MOZILLA_FIVE_HOME="/usr/$(get_libdir)/${PN}"
	cp "${FILESDIR}/mozconfig_trunk" "${S}/.mozconfig" || die "mozconfig missing"
	
	#echo "ac_add_options --with-default-mozilla-five-home=${MOZILLA_FIVE_HOME}" >> "${S}/.mozconfig"
	
	# remove -fstack-protector because now it borks firefox
	CFLAGS=${CFLAGS/-fstack-protector-all/}
	CFLAGS=${CFLAGS/-fstack-protector/}
	CXXFLAGS=${CXXFLAGS/-fstack-protector-all/}
	CXXFLAGS=${CXXFLAGS/-fstack-protector/}

	append-flags -freorder-blocks -fno-reorder-functions

	# Export CPU_ARCH_TEST  as it is not exported by default.
	case $(tc-arch) in
	amd64) [[ ${ABI} == "x86" ]] && CPU_ARCH_TEST="x86" || CPU_ARCH_TEST="x86_64" ;;
	*) CPU_ARCH_TEST=$(tc-arch) ;;
	esac
	
	CPPFLAGS="${CPPFLAGS} -DGENTOO_NSPLUGINS_DIR=/usr/$(get_libdir)/nsplugins -DGENTOO_NSBROWSER_PLUGINS_DIR=/usr/$(get_libdir)/nsbrowser/plugins"
	econf || die "config failed"
	emake -j1 || die "make failed"
}
src_install() {
	declare MOZILLA_FIVE_HOME=/usr/$(get_libdir)/${PN}

	# Most of the installation happens here
	dodir ${MOZILLA_FIVE_HOME}
	#cp -RL --no-preserve=links ${S}/dist/bin/* ${D}${MOZILLA_FIVE_HOME}

	# Create directory structure to support portage-installed extensions.
	# See update_chrome() in mozilla-launcher
	#keepdir ${MOZILLA_FIVE_HOME}/chrome.d
	#keepdir ${MOZILLA_FIVE_HOME}/extensions.d
	#cp ${D}${MOZILLA_FIVE_HOME}/chrome/installed-chrome.txt \
	#	${D}${MOZILLA_FIVE_HOME}/chrome.d/0_base-chrome.txt

	# Create /usr/bin/thunderbird
	install_mozilla_launcher_stub thunderbird ${MOZILLA_FIVE_HOME}

	# Install icon and .desktop for menu entry
	insinto /usr/share/pixmaps
	doins ${FILESDIR}/icon/thunderbird-icon.png

	# Fix bug 54179: Install .desktop file into /usr/share/applications
	# instead of /usr/share/gnome/apps/Internet (18 Jun 2004 agriffis)
	insinto /usr/share/applications
	doins ${FILESDIR}/icon/mozillathunderbird.desktop

	####################################
	#
	# Install files necessary for applications to build against firefox
	#
	####################################

	ewarn "Installing includes and idl files..."
	#dodir ${MOZILLA_FIVE_HOME}/idl ${MOZILLA_FIVE_HOME}/include
	#cd ${S}/dist
	#cp -LfR include/* ${D}${MOZILLA_FIVE_HOME}/include || die "failed to copy"
	#cp -LfR idl/* ${D}${MOZILLA_FIVE_HOME}/idl || die "failed to copy"

	# Dirty hack to get some applications using this header running
	#dosym ${MOZILLA_FIVE_HOME}/include/necko/nsIURI.h \
	#	/usr/$(get_libdir)/${MOZILLA_FIVE_HOME##*/}/include/nsIURI.h
	make DESTDIR=${D} install || die "install failed"
}

pkg_postinst(){
	declare MOZILLA_FIVE_HOME=/usr/$(get_libdir)/${PN}

	update_mozilla_launcher_symlinks
	fdo-mime_desktop_database_update
	echo  ""
	ewarn "DO NOT SUBMIT ANYTHING TO http://bugs.gentoo.org ABOUT THIS EBUILD"
	ewarn "This ebuild is a cvs based build and may fail to compile due to upstream changes"
	ewarn "Thank you! b33fc0d3@gmail.com"
	echo  ""

	epause 15
}

pkg_postrm() {
	declare MOZILLA_FIVE_HOME=/usr/$(get_libdir)/${PN}

	update_mozilla_launcher_symlinks
}
